/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.NewActionHelp;
import com.ciko.code.bean.UserBean;
import com.shwread.android.qysw.activity.BaseBusinessActivity;
import com.shwread.android.qysw10000136.R;
import com.shwread.http.base.TextHttpResponseHandler;
import com.shwread.http.util.JSONUtil;
import com.shwread.httpsdk.http.callback.ActionListener;
import com.shwread.httpsdk.util.Base64;
import com.tgx.tina.android.ipc.framework.IUIPage;

import org.apache.http.Header;
import org.json.JSONException;

import base.tina.core.task.infc.ITaskResult;
import logic.constants.Const;
import logic.dao.UserDao;
import logic.util.Util;

/**
 * @Project： Lqyd_Android
 * @Title：ChangePassword
 * @Description：修改密码
 * @Author：zhangnannan
 * @Date：2014年8月14日下午15:18
 */
public class ChangePasswordPage extends AbstractUIPage<BaseBusinessActivity> implements OnClickListener, ActionListener {

    private EditText etOldPwd, etNewPwd;
    private TextView tvSubmit, tvTitle;
    private Button btnBack;
    private static ChangePasswordPage instance;
    private InputMethodManager inputMethodManager;
    private String oldPwd;
    private String newPwd;

    private LinearLayout llayShowPwd;
    private ImageView ivShowPwd;
    private TextView tvShowPwd;
    private boolean Is_SHOW_PWD = false;


    public static ChangePasswordPage getInstance(BaseBusinessActivity t) {
        /*if (null == instance) */
        instance = new ChangePasswordPage(t);
        return instance;
    }

    public ChangePasswordPage(BaseBusinessActivity t) {
        super(t);
    }

    @Override
    public <E extends Activity> View createView(E arg0, int arg1) {
        if (null != curMyView) return curMyView;

        curMyView = bActivity.getLayoutInflater().inflate(R.layout.user_info_setting_change_pwd, null);
        inputMethodManager = (InputMethodManager) bActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        initView();
        setLinstener();

        return curMyView;
    }

    private void setLinstener() {
        btnBack.setOnClickListener(this);
        tvSubmit.setOnClickListener(this);
        ivShowPwd.setOnClickListener(this);
//		llayShowPwd.setOnClickListener(this);
//		tvShowPwd.setOnClickListener(this);
    }

    private void initView() {
        tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
        tvTitle.setText("修改密码");
        btnBack = (Button) curMyView.findViewById(R.id.common_header_back);
        btnBack.setVisibility(View.VISIBLE);

        etOldPwd = (EditText) curMyView.findViewById(R.id.user_info_setting_change_pwd_old_pwd_et);
        etNewPwd = (EditText) curMyView.findViewById(R.id.user_info_setting_change_pwd_new_pwd_et);
        // etNewPwdAgain = (EditText)
        // curMyView.findViewById(R.id.user_info_setting_change_pwd_new_pwd_again_et);
        tvSubmit = (TextView) curMyView.findViewById(R.id.user_info_setting_change_pwd_submit_tv);

        llayShowPwd = (LinearLayout) curMyView.findViewById(R.id.user_info_setting_change_pwd_show_pwd_llay);
        ivShowPwd = (ImageView) curMyView.findViewById(R.id.user_info_setting_change_pwd_show_pwd_iv);
        tvShowPwd = (TextView) curMyView.findViewById(R.id.user_info_setting_change_pwd_show_pwd_tv);

    }

    private void clear() {
        etOldPwd.setText("");
        etNewPwd.setText("");
    }

    @Override
    public int enter(IUIPage<?> arg0) {
        clear();
        etOldPwd.requestFocus();
        inputMethodManager.showSoftInput(etOldPwd, InputMethodManager.RESULT_UNCHANGED_SHOWN);
        //密文显示
        ivShowPwd.setSelected(false);
        etNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        return 0;
    }

    @Override
    public int leave(IUIPage<?> arg0) {
        Util.hideInputMethodManager(context, etOldPwd);
        Util.hideInputMethodManager(context, etNewPwd);
        return 0;
    }

    @Override
    public void notifyView(int arg0, Bundle arg1) {

    }

    @Override
    public void setStatus(int arg0) {

    }

    @Override
    public View updateView(int arg0, ITaskResult arg1) {
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_header_back:
                bActivity.onBackPressed();
                break;
            case R.id.user_info_setting_change_pwd_submit_tv:
                clickChange();
                break;
            case R.id.user_info_setting_change_pwd_show_pwd_iv:
                clickShowPwd();
                break;
            default:
                break;
        }
    }

    private void clickShowPwd() {
        if (Is_SHOW_PWD) {
            //切换成密文显示
            Is_SHOW_PWD = false;
            ivShowPwd.setSelected(false);
            etNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            //切换成明文显示
            Is_SHOW_PWD = true;
            ivShowPwd.setSelected(true);
            etNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    void clickChange() {
        oldPwd = etOldPwd.getText().toString().trim();
        newPwd = etNewPwd.getText().toString().trim();
        if (Util.isEmpty(oldPwd)) {
            Util.showToast(bActivity, "请输入当前密码");
        } else if (Util.isEmpty(newPwd)) {
            Util.showToast(bActivity, "请输入新密码");
        } else if (Util.NUllToString(newPwd).length() < 6 || Util.NUllToString(newPwd).length() > 16) {
            Util.showToast(bActivity, "密码是6-16位哦");
        } else {
//			new ModPasswordAction(context, oldPwd, newPwd, ChangePasswordPage.this).start();
            NewActionHelp.modPassword(bActivity, oldPwd, newPwd, new MyHandler());
        }

    }

    class MyHandler extends TextHttpResponseHandler {

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            if (statusCode > 1000) {
                if (statusCode == 1103) {
                    Util.showToast(bActivity, "当前密码错误");
                } else {
                    try {
                        Util.showToast(bActivity, JSONUtil.getString(responseString));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            Util.showToast(bActivity, "新密码设置成功");
            if (Util.isNotEmpty(Const.phone_number) && Util.isNotEmpty(newPwd)) {
                // 更新数据库用户密码
                UserDao.getInstance(bActivity).updateUserPwd(
                        new UserBean(Const.phone_number, Base64.encode(newPwd, "utf-8")));
            }
            bActivity.onBackPressed();

        }

    }


    @Override
    public void OK(Object obj) {
        bActivity.getUIHandler().post(new Runnable() {
            @Override
            public void run() {
                Util.showToast(bActivity, "新密码设置成功");
                if (Util.isNotEmpty(Const.phone_number) && Util.isNotEmpty(newPwd)) {
                    // 更新数据库用户密码
                    UserDao.getInstance(bActivity).updateUserPwd(
                            new UserBean(Const.phone_number, Base64.encode(newPwd, "utf-8")));
                }
                bActivity.onBackPressed();
            }
        });
    }

    @Override
    public void ERROR(final int resultCode, final String message) {
        bActivity.getUIHandler().post(new Runnable() {

            @Override
            public void run() {
                if (resultCode == 1103) {
                    Util.showToast(bActivity, "当前密码错误");
                } else {
                    Util.showToast(bActivity, message);
                }
            }
        });
    }

    @Override
    public void HTTPERROR(int statusCode) {

    }

}
